#!/bin/bash
#(c) Copyright Nov 2007 Barry Kauler www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#a complete rewrite for Puppy 'Dingo', Nov 27 2007, Dec 1,5 2007.
#v3.95 4jan2007: -x option for rox.
#v3.96 17jan2008: full install, mounted '/' partition was not shown.
#v3.99 7apr2008: fix zombie process prevents partition unmount.
#v4.01 11may2008: passed params sd,sr,mm,fd when called from pup_dock_xx tray applets.
#v4.01 13may2008: introduce 'preferences' button, add toggle tabbed-normal view.
#v4.01 17may2008: make sure top tab stays on top when mount/unmount.
#v4.01 18may2008: auto launch of pmount when a drive plugged is now confgurable, see prefs.
#v4.02 31may2008: refresh desktop icons when mount/unmount (see pup_eventd).
#v411 added 'quiet' option for vfat, suppresses error msg when file attribs not preserved on copy.
#110208 zigbert: improve gui layout. bk: put this on-hold for now, see 3 commented lines.
#110505 support sudo for non-root user.
#110720 should not open a layer inside aufs, ex: session saved entire partition, desktop drive icon is /initrd/pup_rw.
#110814 01micko: convert script from gtkdialog2 to gtkdialog3+.
#120129 karl godt: fix ZOMBIES, see http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420. see also rc.shutdown, drive_all, pup_event_frontend_d.
#120323 partial replace 'gxmessage' with 'pupmessage'.
#120411 zigbert: gui rewrite, volume-lables, integration of partview, mount at boot
#130128 convert "technosaurus method" i18n to gettext. mut2 option removed (very old, no longer works properly).
#130128 fixed for 'udf' f.s. (see also pup_event_frontend_d, drive_all, probepart).
#130205 zigbert: correct size of bar to report used space on filesystem.
#130216 support f2fs.
 
[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@} #110505

grep -q -m1 -i 'rox' /usr/local/bin/defaultfilemanager && export ROX_FILER=yes

#NOTE: not to clash with 'pmount.mo' used by previous i18n method.
export TEXTDOMAIN=pupmount
export OUTPUT_CHARSET=UTF-8
. gettext.sh

#using ${$} which is pid of script...
MYPID=${$}

. /etc/rc.d/PUPSTATE #v3.96
. /etc/rc.d/functions_x #fx_* functions
export PMOUNT="pmount" #name of this executable.
. /usr/local/pup_event/frontend_funcs #rox

[ -d /tmp/pmount ] || mkdir /tmp/pmount

#build script to reload gui from mountme funtion
echo 'TMP="`busybox ps`"; for I in `echo "$TMP" | grep -F "pmount" | awk '"'"'{print $1}'"'"'`; do kill -9 $I 2> /dev/null; done; exec /usr/sbin/'"$PMOUNT $TOPACTUAL" > /tmp/pmount/pmount_reload
chmod 722 /tmp/pmount/pmount_reload
#---

#read config - this does not include options from the preferences window
[ -f $HOME/.pmountrc ] && . $HOME/.pmountrc
if [ ! "$HEIGHT" ]; then
	HEIGHT=350 
	WIDTH=500
	export COLOR_SPACEFREE='#3FA04E'
	export COLOR_SPACEUSED='#D7302A'
	export COLOR_SPACELOCKED='#EDBF4D'
fi

###################################################################################
#                                                                                 #
#                              F U N C T I O N S                                  #
#                                                                                 #
###################################################################################


buildheading (){
	ONEDISK="$1"
	ONECATEGORY="$2"
	ONEDISKDESCR="$3"
	ONEDISKSIZE="$4"
	ONEDISKNAME=${ONEDISK##*/} #basename $ONEDISK
	
 	case $ONECATEGORY in
		drive)   ICON=/usr/local/lib/X11/pixmaps/drive48.png  ;;
		usbdrv)  ICON=/usr/local/lib/X11/pixmaps/usbdrv48.png ;;
		card)    ICON=/usr/local/lib/X11/pixmaps/card48.png   ;;
		optical) ICON=/usr/local/lib/X11/pixmaps/optical48.png ; PARTSIZE='' ;;
		floppy)  ICON=/usr/local/lib/X11/pixmaps/floppy48.png  ; PARTSIZE='' ;;
	esac
	if [ "`echo $ONECATEGORY | grep -E "optical|usbdrv|card"`" ] && [ "`mount | grep "^$ONEDISK"`" ]; then
		PIXLOCK='<circle cx="18" cy="18" r="14" style="fill:#F1AB24;stroke:#875900;stroke-width:2;fill-opacity:0.8;stroke-opacity:0.6"/>
  <path fill="#333333" d="m 19,8 c -4,0 -6,2 -6,4 0,1 0,3 0,3 0,0 -1,0 -2,0 0,2 0,5 0,8 0,2 2,3 4,3 3,0 5,0 8,0 1,0 3,-1 3,-3 0,-3 0,-5 0,-8 -1,0 -2,0 -2,0 l 0,-3 C 24,9 21,8 19,8 z m 3,5 0,2 -7,0 c 0,-3 0,-2 0,-2 0,-2 1,-3 4,-3 1,0 3,1 3,3 z m -3,4 c 1,0 2,2 0,3 l 1,4 -3,0 1,-4 c -2,-1 -1,-3 1,-3 z"/>'
	else
		PIXLOCK=''
	fi
	echo '
	<svg version="1.0" width="350" height="60" id="svg1"
	  xmlns:xlink="http://www.w3.org/1999/xlink">

	  <defs
		 id="defs4">
		<linearGradient id="LGD_01">
		  <stop style="stop-color:#111;stop-opacity:1" offset="0" />
		  <stop style="stop-color:#111;stop-opacity:0" offset="1" />
		</linearGradient>
		<linearGradient id="LG_01"
		   x1="-10" y1="-40" x2="20" y2="80"	  
		   xlink:href="#LGD_01"
		   gradientUnits="userSpaceOnUse" />
	  </defs>

	  <rect
		 style="fill:url(#LG_01);fill-opacity:1;fill-rule:evenodd;stroke-width:0;stroke:#404040;stroke-opacity:1;stroke-dasharray:none;"
		 width="350" height="60"/>
	  <image xlink:href="'${ICON}'" width="48" height="48" x="5" y="5"/>
	  '"$PIXLOCK"'
	  <text
		 style="fill:#111;stroke:none;font-family:DejaVu;font-style:normal;font-weight:medium;font-size:16;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;"
		 x="75" y="20">
		 '${ONEDISKDESCR}'
	  </text>
	  <text
		 style="fill:#111;stroke:none;font-family:DejaVu;font-style:normal;font-weight:medium;font-size:12;fill-opacity:1;stroke-opacity:1;stroke-width:3pt;stroke-linejoin:miter;stroke-linecap:butt;text-anchor:start;writing-mode:lr;"
		 x="75" y="40">
		 '$ONEDISKSIZE'
	  </text>
	</svg>' > /tmp/pmount/pmount_${ONEDISKNAME}_svg
}

buildpix (){
	ONEDEV="$1"
	ONESIZEK="$2"
	ONECATEGORY="$3"

	DEVPATTERN="^${ONEDEV} "
	ONEMNTPT="`mount | grep "$DEVPATTERN" | cut -f 3 -d ' '`"
	DEVNAME=${ONEDEV##*/} #basename $DEVNAME

	#size
	if [ "$ONEFS" != "cdda" ]; then
		[ "$ONESIZEK" = 0 ] && ONESIZEK=1 #calculation of percent (svg) fails if size is 0 (ie. a blank CD)
		ONESIZE=$( fx_format_bytes $(($ONESIZEK * 1024)) )
	fi

	#free space
	if [ ! "$ONEMNTPT" ]; then
		FREEK=$ONESIZEK
		export PIXTEXT="${ONESIZE}"
	else 
		export FREEK=`df -k | tr -s ' ' | grep -m1 "$DEVPATTERN" | cut -f 4 -d ' '`
		[ ! "$FREEK" ] && FREEK=$ONESIZEK
		ONEFREE=$( fx_format_bytes $(($FREEK * 1024)) )
		export PIXTEXT="${ONESIZE} / ${ONEFREE}"
	fi
	
	if [ "$ONECATEGORY" != "optical" ]; then
		 /usr/lib/gtkdialog/svg_bar 120 "$((($ONESIZEK-$FREEK)*120/$ONESIZEK))" "$PIXTEXT"  > /tmp/pmount/pmount${DEVNAME}_svg
	else
		 /usr/lib/gtkdialog/svg_bar 120 0 "$(gettext 'N/A')"  > /tmp/pmount/pmount${DEVNAME}_svg
	fi
}

volumelabel (){
	DODEV="$1"
	DEVNAME=${1##*/} #basename $1
	local FS=$2
	export inputbox='
	<window title="pMount - '$(gettext 'Puppy Drive Mounter')'">
	<vbox>
	  <frame '$(gettext 'Edit label')' - '$DODEV'>
	    <entry activates-default="true">
		  <variable>VOLUME_LABEL</variable>
		  <input>cat /tmp/pmount/pmount_'${DEVNAME}'_LABEL</input>
	    </entry>
	  </frame>
	  <hbox>
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
	      <label>'$(gettext 'Cancel')'</label>
	      <action type="exit">cancel</action>
	    </button>
	    <button can-default="true" has-default="true" use-stock="true">
          '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
	      <label>'$(gettext 'Ok')'</label>
	      <action type="exit">OK</action>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	eval "`gtkdialog -p inputbox --center`"
	if [ "$EXIT" = "OK" ]; then
		read OLDLABEL < /tmp/pmount/pmount_${DEVNAME}_LABEL
		if [ "$VOLUME_LABEL" = "$OLDLABEL" ] ; then
			return
		fi
		LABELOK=1
		case $FS in
			ext*)  err=$(e2label $DODEV "$VOLUME_LABEL" 2>&1)    || LABELOK='' ;;
			vfat)  err=$(fatlabel $DODEV "$VOLUME_LABEL" 2>&1)   || LABELOK='' ;;
			ntfs)  err=$(ntfslabel $DODEV "$VOLUME_LABEL" 2>&1)  || LABELOK='' ;;
			exfat) err=$(exfatlabel $DODEV "$VOLUME_LABEL" 2>&1) || LABELOK='' ;;
		esac
		if [ "$LABELOK" ] ; then
			echo "$VOLUME_LABEL" > /tmp/pmount/pmount_${DEVNAME}_LABEL
			if [ -e /sys/block/$(fx_get_drvname $DEVNAME)/${DEVNAME}/uevent ] ; then
				echo change > /sys/block/$(fx_get_drvname $DEVNAME)/${DEVNAME}/uevent
			else
				echo change > /sys/block/$(fx_get_drvname $DEVNAME)/uevent
			fi
		else
			/usr/lib/gtkdialog/box_ok "pMount" error "Error:" "" "$err"
		fi
	fi
}

mountme (){
	#         UNMOUNT_/dev/sda2_ext4_/mnt/sda2
	IFS="_" read DOACTION DODEV DOFS MNTPT <<< "$1"
	DEVNAME="`echo -n "$DODEV" | cut -f 3 -d '/'`"
	devPATTERN="^$DEVNAME|"
	TOPACTUAL="`echo "$DEV2TAB" | tr ' ' '\n' | grep "$devPATTERN" | cut -f 2 -d '|'`" #find top tab.
	if [ "$DOACTION" = 'DOMOUNT' ];then
		mkdir -p /mnt/$DEVNAME
		mount -t $DOFS $DODEV /mnt/$DEVNAME
		RETVAL1=$?
		if [ $RETVAL1 -eq 0 ]; then
			defaultfilemanager /mnt/$DEVNAME &
		else
			/usr/lib/gtkdialog/box_ok "pMount - $(gettext 'Puppy Drive Mounter')" error "$(gettext "Unable to mount") $DEVNAME"
			exec /tmp/pmount/pmount_reload
		fi
	else
		[ -z $MNTPT ] && MNTPT=$(mount | grep "^${DODEV} " | cut -f 3 -d ' ')
		[ "$ROX_FILER" = "yes" ] && rox -D $MNTPT
		sync
		umount $DODEV
		umVAL=$?
		#v3.99 fix zombie process prevents partition unmount, invisible to fuser...
		if [ $umVAL -ne 0 ];then
			#120129 karl godt: fix ZOMBIES, see http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420 ...
			ZOMBIES="`ps -H -A | grep '<defunct>' | sed 's/^[[:blank:]]*//;s/  /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
			for ONEZOMBIE in $ZOMBIES; do
				echo "Killing parentless zombie process $ONEZOMBIE"
				kill $ONEZOMBIE
			done
			umount $DODEV ; umVAL=$?
		fi
		#v3.99 rare ocassion, partition got unmounted before umount runs above...
		umPATTERN="${MNTPT} "
		[ "`mount | grep "$umPATTERN"`" = "" ] && umVAL=0

		if [ $umVAL -ne 0 ];then
			EXTRAMSG0="`fuser -v -m $MNTPT`"  
			if [ "$EXTRAMSG0" = "" ];then
				/usr/lib/gtkdialog/box_ok "pMount - $(gettext 'Puppy Drive Mounter')" error "$(gettext 'FAILURE! In the case of removable media, the most common reason is the media is not currently inserted. If so, please remedy.')"
			else
				gxmessage -wrap -bg "#ff99ff" -title "pMount - $(gettext 'Puppy Drive Mounter')" -center -buttons "$(gettext 'KILL')":10,"$(gettext 'EXIT')":20 "$(gettext 'FAILURE!')
$(gettext 'One or more processes (programs) are currently
using the partition. Here they are:')
${EXTRAMSG0}
 
$(gettext "If you press the 'KILL' button, Puppy will attempt to
kill the offending programs for you. Only do this as a
last resort.")
$(gettext "Firstly you should try to close the programs
manually, so do not press 'KILL'.")
$(gettext "PLEASE PRESS THE 'EXIT' BUTTON!")"


				[ $? -eq 10 ] && fuser -k -m $MNTPT
			fi
			exec /tmp/pmount/pmount_reload
		else
			if [ "$DOFS" = "iso9660" ]; then
				eject $DODEV
				/usr/lib/gtkdialog/box_ok "pMount - $(gettext 'Puppy Drive Mounter')" info "$(gettext 'Remove media, then press <b>Ok</b>')"
				eject -t $DODEV
				exec /tmp/pmount/pmount_reload
			fi
		fi
	fi
}

writeconfig (){
	while read -r F1 F2 F3 F4 F5
	do
		case $F1 in
			"Width:")  WIDTH=$F2  ;;
			"Height:") HEIGHT=$F2 ;;
			Absolute)
				[ "$F3" = "X:" ] && X1=$F4
				[ "$F3" = "Y:" ] && Y1=$F4
				;;
			Relative)
				[ "$F3" = "X:" ] && X2=$F4
				[ "$F3" = "Y:" ] && Y2=$F4
				;;
		esac
	done <<EOF
`xwininfo -stats -name pMount`
EOF
	X=$(($X1-$X2))
	Y=$(($Y1-$Y2))
	(
	echo "#pMount config"
	echo "export COLOR_SPACEFREE='#3FA04E'"
	echo "export COLOR_SPACEUSED='#D7302A'"
	echo "export COLOR_SPACELOCKED='#EDBF4D'"
	echo -e "\n#Geometry"
	echo "export HEIGHT=\"$HEIGHT\""
	echo "export WIDTH=\"$WIDTH\""
	echo "export X=\"$X\""
	echo "export Y=\"$Y\""
	) > $HOME/.pmountrc
}

export -f buildheading buildpix volumelabel mountme writeconfig





###################################################################################
#                                                                                 #
#                                P R O G R A M                                    #
#                                                                                 #
###################################################################################


/usr/lib/gtkdialog/box_splash -close never -fontsize large -text "$(gettext 'Please wait, probing hardware...')" & #110720
X1PID=$!

. /etc/rc.d/functions_x
drv=$(fx_get_drvname $1)
if [ -n "$drv" -a -e /sys/block/${drv} ] ; then
	#drive passed as a param
	shift 1
	PROBEPART="probepart -k -extra-info ${drv}.*"
	PROBEDISK="probedisk -plus-size -hr $drv"
	SINGLE_DRV=1
	DRV_PARAM=$drv
else
	PROBEPART="probepart -k -extra-info"
	PROBEDISK="probedisk -plus-size -hr"
fi

#v4.01 passed param, choose which tab to be "on top"...
TOPTAB=''
if [ $1 ];then
 TOPTAB="$1" #also allow full tab names usbdrv,optical,drive,card,floppy to be passed.
 [ "$1" = "any" ] && TOPTAB="drive" #see pup_eventd
 #pup_eventd calls pmount with just two letters...
 [ "$1" = "sd" ] && TOPTAB="usbdrv"
 [ "$1" = "sr" ] && TOPTAB="optical"
 [ "$1" = "mm" ] && TOPTAB="card"
 [ "$1" = "fd" ] && TOPTAB="floppy"
fi

#v4.01 toggle tabbed interface...
SINGLEFLAG="true" #default tabbed interface.
[ -f /root/.pmountsingle ] && SINGLEFLAG="`cat /root/.pmountsingle`"

#v3.96 'mount' misreports which partition mounted on '/'...
ROOTDEV2=""
if [ "$PUPMODE" = "2" ] ; then
 ROOTDEV2="`df | grep ' /$' | grep '^/dev/' | cut -f 1 -d ' '`"
 if [ "$ROOTDEV2" = "/dev/root" ] ; then # raspberry pi
  ROOTDEV2="$(expr match "`cat /proc/cmdline`" '.*root=\([^ ]*\)')"
 fi
fi

PROBEPARTS="`$PROBEPART`"

DISKINFO="`$PROBEDISK | sort -k 2 --field-separator='|'`"
PARTSINFO="`echo "$PROBEPARTS" | grep -v 'none'`"

if [ ! "$SINGLE_DRV" ] ; then
	PARTS2IGNORE="`echo "$PROBEPARTS" | grep 'none' | cut -f 1 -d '|'`"
	for i in $PARTS2IGNORE ;do
		DISKINFO="$(echo "$DISKINFO" | grep -v "${i}|")"
	done
fi

if [ "$SINGLE_DRV" != "1" ] ; then
	#add floppy drive, if any (note, zip detected by probedisk/probepart)...
	if [ -e /sys/block/fd0 ];then
		DISKINFO="/dev/fd0|floppy|floppy disk drive
$DISKINFO"
		PARTSINFO="/dev/fd0|vfat|1440
$PARTSINFO"
	fi
fi

DO_NOT_REMOVE_MSG=$(gettext 'DO NOT REMOVE MOUNTED MEDIA')
if [ "$SINGLE_DRV" = "1" ] ; then
	if [ -z "$(echo $PARTSINFO)" ] ; then
		DO_NOT_REMOVE_MSG="$drv has no known partitions"
	fi
fi

#v4.01 reorganise so that a particular tab is on top...
if [ "$TOPTAB" != "" ];then
 toptabPATTERN='|'"$TOPTAB"'|'
 TOPDISKS="`echo "$DISKINFO" | grep "$toptabPATTERN"`"
 if [ "$TOPDISKS" != "" ];then
  DISKINFO="${TOPDISKS}
`echo "$DISKINFO" | grep -v "$toptabPATTERN"`"
 fi
fi

#v4.01 find the tabs...
#probedisk2 has: drive, optical, usbdrv, card, floppy
TABLIST="`echo -n "$DISKINFO" | cut -f 2 -d '|' | uniq | tr '\n' '|' | sed -e 's/|$//'`"
CURRENTTAB=''

#v408 v410 moved up...  130216 added f2fs...
VALIDPARTS="`echo "$PARTSINFO" | grep -E 'btrfs|ext2|ext3|ext4|f2fs|hfs|iso9660|jfs|minix|msdos|ntfs|reiser|udf|vfat|exfat|xfs'`" #130128

#get actual top tab...
TOPACTUAL="`echo "$TABLIST" | cut -f 1 -d '|'`"

#PARTSINFO has to be sorted in same order as the categories in DISKINFO...
PARTSGUI=""
FRAMEFLAG="off"
export DEV2TAB=""

###############################################################################
CNT=0
while IFS="|" read -r ONEDISK ONECATEGORY ONEDISKDESCR ONEDISKSIZE
do
  [ -z "$ONEDISK" ] && continue
  CURRENT_DRV=$ONEDISK
  if [ "$PREV_DRV" -a "$CURRENT_DRV" != "$PREV_DRV" ] ; then
	#end previous drive info
	PARTSGUI="${PARTSGUI}
		<text height-request=\"15\"><label>\"\"</label></text>
	</vbox>"
  fi
  if [ -z "$PREV_DRV" -o "$CURRENT_DRV" != "$PREV_DRV" ] ; then
	ONEDISKNAME=${ONEDISK##*/} #basename $ONEDISK
	DSKPATTERN="^${ONEDISK}|"
	if [ "$CURRENTTAB" = "" ];then
	  PARTSGUI="${PARTSGUI}<vbox scrollable=\"true\">"
	  TABFLAG="on"
	else
	  if [ "$CURRENTTAB" != "$ONECATEGORY" ] ;then
	    PARTSGUI="${PARTSGUI}</vbox><vbox scrollable=\"true\">"
	  fi
	fi
	CURRENTTAB="$ONECATEGORY"

	buildheading "${ONEDISK}" "${ONECATEGORY}" "${ONEDISKDESCR}" "$ONEDISKSIZE"
	PARTSGUI="${PARTSGUI}
	<vbox spacing=\"0\">
	 <hbox space-expand=\"false\" space-fill=\"false\">
	  <pixmap space-expand=\"false\" space-fill=\"false\"><variable>PIX_DISK_${ONEDISKNAME}</variable><input file>/tmp/pmount/pmount_${ONEDISKNAME}_svg</input></pixmap>
	  <hbox space-expand=\"true\" space-fill=\"true\"><text><label>\"\"</label></text></hbox>
	 </hbox>"
  fi
  PREV_DRV=$CURRENT_DRV
  #====================
  #   partitions
  #====================
  while IFS="|" read -r ONEDEV ONEFS ONESIZE ONELABEL IS_MOUNTED
  do
	[ -z $ONEDEV -o "$ONEDEV" = "none" ] && continue
	CNT=$((CNT+1))
	DEVNAME=${ONEDEV##*/} #basename $ONEDEV

	export DEV2TAB="${DEV2TAB} ${DEVNAME}|${CURRENTTAB}" #remember which tab a partition is in.
	ONEMNTPT="`grep "${ONEDEV} " /proc/self/mountinfo | grep '/ ' | cut -f 5 -d ' '`"
	[ "$ROOTDEV2" != "" ] && [ "$ROOTDEV2" = "$ONEDEV" ] && ONEMNTPT='/' #v3.96

	#in some cases prevent unmounting...
	INVISBUT=""
	[ "$ONEMNTPT" = "/" ] && INVISBUT='<visible>disabled</visible>'
	case $ONEMNTPT in /initrd/*)
		INVISBUT='<visible>disabled</visible>'
		#if 'home' partition, substitute /mnt/home...
		if [ -L /mnt/home ] ; then
			HOMELINK="`readlink /mnt/home`"
			[ "$HOMELINK" = "$ONEMNTPT" ] && ONEMNTPT="/mnt/home"
		fi ;;
	esac

	#should not open a layer inside aufs, ex: session saved entire partition, desktop drive icon is /initrd/pup_rw...
	case $ONEMNTPT in /initrd/pup_*) ONEMNTPT="/" ;; esac #open rox at top of aufs layers.

	#define volume label and action menu/button
	echo '' > /tmp/pmount/pmount_${DEVNAME}_LABEL
	EDITLABEL=false
	MENU_BOOTMOUNT=false
	case $ONEFS in
		ext*|vfat|ntfs|exfat) EDITLABEL=true ;;
	esac
	if [ ! "$ONEMNTPT" ]; then
		INVISSHOWLOC=false
		INVISPIX=false
		INVISFILECHK=true
		echo false > /tmp/pmount/pmount_${DEVNAME}_domount
	else
		INVISSHOWLOC=true
		INVISPIX=true
		INVISFILECHK=false
		echo true > /tmp/pmount/pmount_${DEVNAME}_domount
	fi
	ACTION_MENU="
	<menuitem label=\"$(gettext 'Show location')\" stock=\"gtk-open\">
	 <variable>MENU_SHOWLOC_${DEVNAME}</variable>
	 <sensitive>${INVISSHOWLOC}</sensitive>
	 <action>defaultfilemanager ${ONEMNTPT} &</action>
	</menuitem>"
	if [ "$ONEFS" != "iso9660" ];then
		#check if patrition is mounted at boot
		[ "`grep "^mkdir -p /mnt/${DEVNAME}; mount" /etc/rc.d/rc.local | grep -F '#pmount'`" ] && MENU_BOOTMOUNT=true
		#---
		ACTION_MENU="$ACTION_MENU
		<menuitemseparator></menuitemseparator>
		<menuitem label=\"$(gettext 'Edit label')\" stock=\"gtk-edit\">
		 <sensitive>${EDITLABEL}</sensitive>
		 <action>volumelabel ${ONEDEV} ${ONEFS}</action>
		 <action>refresh:LABEL_${DEVNAME}</action>
		</menuitem>
		<menuitem label=\"$(gettext 'Check filesystem')\" stock=\"gtk-find\">
		 <variable>MENU_FILECHK_${DEVNAME}</variable>
		 <sensitive>${INVISFILECHK}</sensitive>
		 <action>rxvt --hold --title \"pMount - $(gettext 'Puppy Drive Mounter')\" -geometry 80x10 -bg black -fg green -e fsck -Cv \"$ONEDEV\"</action>
		</menuitem>
		<menuitemseparator></menuitemseparator>
		<menuitem checkbox=\"$MENU_BOOTMOUNT\" label=\"$(gettext 'Mount partition at boot')\">
		 <variable>MENU_BOOTMOUNT_${DEVNAME}</variable>
		 ${INVISBUT}
		 <action>cp -f /etc/rc.d/rc.local /tmp/pmount/pmount_tmp</action>
		 <action>if false grep -v '${DEVNAME} #pmount' /tmp/pmount/pmount_tmp > /etc/rc.d/rc.local</action>
		 <action>if true echo 'mkdir -p /mnt/${DEVNAME}; mount -t $ONEFS $ONEDEV /mnt/$DEVNAME #pmount' >> /etc/rc.d/rc.local</action>
		</menuitem>"
	fi
	BUTTON="
	<checkbox draw_indicator=\"false\">
	 <variable>BUTTON_${DEVNAME}</variable>
	 <label>$(gettext '(un) Mount')</label>
	 <default>$(</tmp/pmount/pmount_${DEVNAME}_domount)</default>
	 <input>cat /tmp/pmount/pmount_${DEVNAME}_domount</input>
	 ${INVISBUT}
	 <action>if false mountme UNMOUNT_${ONEDEV}_${ONEFS}_${ONEMNTPT}</action>
	 <action>if false disable:PIX_${DEVNAME}</action>
	 <action>if false disable:MENU_SHOWLOC_${DEVNAME}</action>
	 <action>if false enable:MENU_FILECHK_${DEVNAME}</action>
	 <action>if true mountme DOMOUNT_${ONEDEV}_${ONEFS}</action>
	 <action>if true enable:PIX_${DEVNAME}</action>
	 <action>if true enable:MENU_SHOWLOC_${DEVNAME}</action>
	 <action>if true disable:MENU_FILECHK_${DEVNAME}</action>
	</checkbox>"
	case "$ONEFS" in
		audiocd)
			echo 'CD-Audio' > /tmp/pmount/pmount_${DEVNAME}_LABEL
			ACTION_MENU="<menuitem label=\"$(gettext 'Play')\" stock=\"gtk-media-play\"><action>defaultcdplayer &</action></menuitem>
			<menuitemseparator></menuitemseparator>
			<menuitem label=\"$(gettext 'Eject disc')\" stock=\"gtk-cdrom\"><action>eject ${ONEDEV} &</action><action type=\"exit\">REFRESH</action></menuitem>"
			ONEFS=cdda
			ONESIZE=74min
			BUTTON="<button><label>$(gettext '(un) Mount')</label><sensitive>false</sensitive></button>"
			INVISPIX=true
			;;
		iso9660)
			ACTION_MENU="${ACTION_MENU}
			<menuitemseparator></menuitemseparator>"
			[ "`pburn --help | grep -A10 -F '[MODE]' | grep -F info`" ] && ACTION_MENU="${ACTION_MENU}<menuitem label=\"$(gettext 'Disc information')\" stock=\"gtk-info\"><action>pburn -m info &</action></menuitem>"
			ACTION_MENU="${ACTION_MENU}
			<menuitem label=\"$(gettext 'Play') Video-DVD\" stock=\"gtk-media-play\"><action>defaultmediaplayer dvd:// &</action></menuitem>
			<menuitemseparator></menuitemseparator>
			<menuitem label=\"$(gettext 'Eject disc')\" stock=\"gtk-cdrom\"><action>eject ${ONEDEV} &</action><action type=\"exit\">REFRESH</action></menuitem>"
			[ "$ONEMNTPT" ] && INVISPIX=true
			;;
		swap)
			echo Memory > /tmp/pmount/pmount_${DEVNAME}_LABEL
			export PIXTEXT="${ONESIZE}"
			BUTTON="<button><label>$(gettext '(un) Mount')</label><sensitive>false</sensitive></button>"
			if [ "`grep -Fw "$ONEDEV" /proc/swaps`" ]; then
				ACTION_MENU="<menuitem label=\"$(gettext 'Swap off')\" stock=\"gtk-no\"><action>swapoff $ONEDEV</action><action type=\"exit\">REFRESH</action></menuitem>"
				INVISPIX=true
			else
				ACTION_MENU="<menuitem label=\"$(gettext 'Swap on')\" stock=\"gtk-yes\"><action>swapon $ONEDEV</action><action type=\"exit\">REFRESH</action></menuitem>"
			fi
			;;
	esac
	if [ "$(echo $ONELABEL)" ] ; then
		echo "$ONELABEL" 2> /dev/null > /tmp/pmount/pmount_${DEVNAME}_LABEL
	fi

	buildpix ${ONEDEV} ${ONESIZE} ${ONECATEGORY}

	PARTSGUI="${PARTSGUI}
	<hbox height-request=\"22\">
	 <menubar><menu label=\"${DEVNAME}\" stock=\"gtk-arrow_down\">$ACTION_MENU</menu></menubar>
	 <text space-expand=\"false\" space-fill=\"false\"><variable>LABEL_${DEVNAME}</variable><input>cat /tmp/pmount/pmount_${DEVNAME}_LABEL</input></text>"
	 [ "$INVISBUT" ] && PARTSGUI="${PARTSGUI}<pixmap space-expand=\"false\" space-fill=\"false\"><input file>/usr/share/pixmaps/puppy/puppy.svg</input><height>20</height></pixmap>"
	 
	 PARTSGUI="${PARTSGUI}
	 <hbox space-expand=\"true\" space-fill=\"true\"><text><label>\"\"</label></text></hbox>
	 <text><label>${ONEFS}</label></text>
	 <vbox spacing=\"0\">
	  <text height-request=\"2\"><label>\"\"</label></text>
	  <pixmap><variable>PIX_${DEVNAME}</variable><sensitive>$INVISPIX</sensitive><input file>/tmp/pmount/pmount${DEVNAME}_svg</input></pixmap>
	 </vbox>
	 $BUTTON
	</hbox>
	<text height-request=\"2\"><label>\"\"</label></text>"
  done <<< "$(echo "$PARTSINFO" | grep "^${ONEDISK}")"
done <<< "$DISKINFO"

if [ $CNT -gt 0 ] ; then
	PARTSGUI="${PARTSGUI}
<text height-request=\"15\"><label>\"\"</label></text>
</vbox>"
fi

if [ "$TABFLAG" = "on" ];then #v4.01
 PARTSGUI="${PARTSGUI}</vbox>"
fi

PREFSBUTTON='
<button space-expand="false" space-fill="false">
  '"`/usr/lib/gtkdialog/xml_button-icon preferences`"'
  <action type="exit">SETPREFS</action>
</button>'

m_03="$(gettext 'drive')"
m_04="$(gettext 'usbdrv')"
m_05="$(gettext 'optical')"
m_06="$(gettext 'card')"
m_07="$(gettext 'floppy')"
M_TABLIST=$(echo $TABLIST|sed -e "s/drive/$m_03/" -e "s/usbdrv/$m_04/" -e "s/optical/$m_05/" -e "s/card/$m_06/" -e "s/floppy/$m_07/")
if [ "$SINGLEFLAG" = "false" ];then #default
 NOTEBOOKHEAD="<notebook space-expand=\"true\" space-fill=\"true\" labels=\"${M_TABLIST}\">"
 NOTEBOOKTAIL="</notebook>"
else
 NOTEBOOKHEAD="<vbox scrollable=\"true\">"
 NOTEBOOKTAIL="</vbox>"
 PARTSGUI="`echo "$PARTSGUI" | sed  's/ scrollable="true"//g'`"
fi

export PMOUNTGUI='
<window title="pMount" icon-name="gtk-harddisk" default_height="'$HEIGHT'" default_width="'$WIDTH'">
<vbox>
  
 '${NOTEBOOKHEAD}'
 '${PARTSGUI}'
 <hbox homogeneous="true" space-expand="true" space-fill="true">
   <text use-markup="true" space-expand="true" space-fill="true"><label>"<span fgcolor='"'#821811'"'><b>'${DO_NOT_REMOVE_MSG}'</b></span>"</label></text>
 </hbox>
 '${NOTEBOOKTAIL}'

  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon refresh`"'
      <label>'$(gettext 'Refresh')'</label>
      <action>writeconfig</action>
      <action type="exit">REFRESH</action>
    </button>
   '${PREFSBUTTON}'
   <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
     <action>writeconfig</action>
     <action type="exit">EXIT</action>
   </button>
   '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
 </vbox>
 <action signal="delete-event">writeconfig</action>
</window>'
echo "$PMOUNTGUI" > /tmp/pmount/pmountdlg.txt_${MYPID}
kill $X1PID

#gtk theme
echo 'pixmap_path "/usr/share/pixmaps/puppy/"
style "pmount"
{ stock["gtk-arrow_down"] = {{ "arrow_mini_down.svg", *, *, *}}
GtkStatusbar::shadow_type = GTK_SHADOW_NONE }
class "GtkWidget" style "pmount"' > /tmp/pmount/gtkrc_pmount
export GTK2_RC_FILES=/tmp/pmount/gtkrc_pmount:$HOME/.gtkrc-2.0

#v4.01...
[ ! -e /tmp/pmount/gtkdialog_pmount ] && ln -s /usr/sbin/gtkdialog /tmp/pmount/gtkdialog_pmount
/tmp/pmount/gtkdialog_pmount --file=/tmp/pmount/pmountdlg.txt_${MYPID} -G +"$X"+"$Y" >/tmp/pmount/pmountdlg_${MYPID} 2>/tmp/pmount/pmounterr


###################################################################################
#                                                                                 #
#                          P O S T   O P E R A T O I N S                          #
#                                                                                 #
###################################################################################

rm -f /tmp/pmount/pmountdlg.txt_${MYPID}
#if gui killed by pup_eventd, then file will be empty. restart pmount...
if [ ! -s /tmp/pmount/pmountdlg_${MYPID} ];then
 rm -f /tmp/pmount/pmountdlg_${MYPID}
 exit
fi
RETPARAMS="`cat /tmp/pmount/pmountdlg_${MYPID}`"
eval "$RETPARAMS"

[ "$EXIT" = "REFRESH" ] && exec /usr/sbin/$PMOUNT $TOPACTUAL

if [ "$EXIT" = "SETPREFS" ];then
 DLGSETENG='
 <window title="pMount - '$(gettext 'Preferences')'">
 <vbox space-expand="true" space-fill="true">
   <frame '$(gettext 'Preferences')'>
     <text xpad="10" xalign="0" space-expand="true" space-fill="true"><label>'$(gettext "If you don't have many drives or partitions, you may find it more convenient to see everything in one window. Untick the box if you want the default tabbed interface, or tick for a single window.")'</label></text>
     <checkbox>
       <label>'$(gettext 'Tick box for single window')'</label>
       <default>'${SINGLEFLAG}'</default>
       <variable>NEWSINGLEFLAG</variable>
     </checkbox>
   </frame>
   <hbox>
     <button>
       '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
       <label>'$(gettext 'Cancel')'</label>
     </button>
     <button>
       '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
       <label>'$(gettext 'Ok')'</label>
       <action>exit:OK</action>
     </button> 
   </hbox>
</vbox>
</window>'

 RETPARAMS="`echo "$DLGSETENG" | gtkdialog --stdin`"
 eval "$RETPARAMS"
 if [ "$EXIT" = "OK" ];then
  echo "$NEWSINGLEFLAG" > /root/.pmountsingle
 fi
 exec /usr/sbin/$PMOUNT $TOPACTUAL
fi

###END###
